package com.atguigu.realtime.app;

import com.atguigu.realtime.util.FlinkSourceUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/2/9 9:53
 */
public abstract class BaseAppV2 {
    
    protected abstract void run(StreamExecutionEnvironment env,
                                Map<String, DataStreamSource<String>> topicStreamMap);
    
    protected void init(int port,
                        int p,
                        String ck,
                        String groupId,
                        String topic,  // 调用函数的时候, 至少传递一个topic过来
                        String... otherTopics) {
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", port);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(p);
        
        // 开启checkpoint
        env.enableCheckpointing(3000);
        // 设置状态后端
        env.setStateBackend(new HashMapStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/gmall/" + ck);
        
        // checkpoint 的一些其他设置
        env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointTimeout(30 * 1000);
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);
        env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
        
        ArrayList<String> topics = new ArrayList<>();
        topics.add(topic);
        topics.addAll(Arrays.asList(otherTopics));
        
        Map<String, DataStreamSource<String>> topicStreamMap = new HashMap<>();
        //遍历所有表的topic, 每个topic对应一个流
        for (String t : topics) {
            DataStreamSource<String> stream = env.addSource(FlinkSourceUtil.getKafkaSource(groupId, t));
            topicStreamMap.put(t, stream);
        }
        run(env, topicStreamMap);
        try {
            env.execute(ck);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
}
